﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Boomerang.Common.Crm;
using Boomerang.Common.Configuration;
using Boomerang.Management.UIForms;

namespace Boomerang.Management
{
    public partial class OrganizationForm : Form
    {
        public Organization CrmOrganization { get; set; }

        public OrganizationForm()
        {           
            InitializeComponent();
            this.CrmOrganization = new Organization();
        }

        public OrganizationForm(Organization org)
        {
            InitializeComponent();
            logonControlCrm.Options = org.ConnectionOptions;
            this.CrmOrganization = org;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                Connection crmConnection = new Connection(logonControlCrm.Options);
                if (!crmConnection.Test()) throw new ApplicationException("The connection details are not correct. No user found");
                this.DialogResult = DialogResult.OK;

                this.CrmOrganization.ConnectionOptions = logonControlCrm.Options;
                this.CrmOrganization.Name = logonControlCrm.Options.OrgName;

                this.Close();
            }
            catch (Exception ex)
            {
                ExceptionBox.Show(this, ex, "An error ocurred");                
            }            
        }

    }
}
